﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Xml.Linq;

using ProjectBase.Core;

namespace ProjectBase.Tools.Publishing
{
    /// <summary>
    ///     Default PublishingProvider.
    ///     Handles requests by configurable set of holders
    /// </summary>
    public class DefaultPublishingProvider : PublishingProviderBase
    {
        public virtual IPublishingHandler Handler { get; protected set; }
        public override void Init(XElement settings)
        {
            var configurator = new HandlerConfigurator();
            var handlers = configurator.CreateHandlers(settings);
            Handler = configurator.CreateHandlersChain(handlers.OrderBy(h => h.Priority));
        }
        public override void Shutdown()
        {
            if (Handler.Is())
            {
                Handler.Exit();
            }
            base.Shutdown();
        }
        public override void Publish(object reporter, string message, Exception exception)
        {
            Handler.Handle(reporter, message, exception);
        }
        public override void Publish(object reporter, Severity severity, object message, params object[] args)
        {
            Handler.Handle(reporter, severity, message, args);
        }
        public override void Publish(object reporter, Func<string> message, Severity severity)
        {
            Handler.Handle(reporter, message, severity);
        }
    }
}